<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="订单编号" field="order_sn">
            <a-input v-model="searchForm.order_sn" placeholder="请输入订单编号" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="订单状态" field="status">
            <sa-select v-model="searchForm.status" dict="pay_status" placeholder="请选择订单状态" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="订单类型" field="type">
            <sa-select v-model="searchForm.type" dict="bill_type" placeholder="请选择订单类型" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="交易账户" field="account">
            <a-input v-model="searchForm.account" placeholder="请输入交易账户" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="16">
          <a-form-item label="创建时间" field="create_time">
            <a-range-picker v-model="searchForm.create_time" :show-time="true" mode="date" style="width: 100%" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <!-- 操作列 -->
      <template #operationBeforeExtend="{ record }">
        <a-link type="primary" @click="detailAction(record)"> <icon-eye />详情 </a-link>
      </template>
    </sa-table>

    <!-- 详情 -->
    <detail-form ref="detailRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import DetailForm from './detail.vue'
import api from '../../api/app/bill'

// 引用定义
const crudRef = ref()
const detailRef = ref()
const optionData = reactive({})

// 搜索表单
const searchForm = ref({
  create_time: [],
  order_sn: '',
  status: '',
  type: '',
})

const detailAction = async (record) => {
  detailRef.value?.open('detail')
  detailRef.value?.setFormData(record)
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  delete: {
    show: true,
    auth: ['/app/saiuser/app/AppBill/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
  { title: '订单编号', dataIndex: 'order_sn', width: 180 },
  { title: '交易用户', dataIndex: 'user.account', width: 120 },
  { title: '订单状态', dataIndex: 'status', type: 'dict', dict: 'app_pay_status', width: 100 },
  { title: '订单类型', dataIndex: 'type', type: 'dict', dict: 'app_bill_type', width: 100 },
  { title: '订单金额', dataIndex: 'money', width: 100 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
